package cn.chainco.cts.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;

import cn.chainco.cts.iservice.CommonManager;
import cn.chainco.cts.iservice.IDataDictionaryManager;
import cn.chainco.cts.jdo.DataDictionary;
import cn.chainco.cts.jdo.Work;
import cn.chainco.cts.utils.CommUtils;
import cn.chainco.cts.utils.CookieUtil;
import cn.chainco.cts.utils.query.Page;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
@InterceptorRef("ctsInterceptors")
public class DataDictionaryAction  extends ActionSupport {
	private IDataDictionaryManager dataDictionaryManager;
	private DataDictionary dataDictionary;
	private CommonManager commonManager;
	
	private String keyword;
	private Page pager;
	private int pageSize=15;
	private int pageNumber=1;

	/**
	 * 查找
	 * @return
	 */
	@Action("findDataDicationaryList")
	public String findDataDicationaryList(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String type = request.getParameter("type");
		String typeDetail = request.getParameter("typeDetail");
		
		String toOrder = request.getParameter("toOrder"); // 是否到排序页面
		String status = request.getParameter("status");
		String workKey = request.getParameter("workKey");
		String ctstype = request.getParameter("ctstype");
		
		if(type==null || type==""){
			type = "1";
		}
		
		if(typeDetail==null || typeDetail==""){
			typeDetail = "1";
		}
		
		List<DataDictionary> list = null;
		try {
			list = this.dataDictionaryManager.findDataByType(workKey , type , typeDetail , status, "2");
			/*if(list.size() > 0){
				String language = CookieUtil.getCookieValue(request, "cts_locale");
				for (DataDictionary dictionary : list) {
					if(language.equals("zh_CH")){
						dictionary.setName(dictionary.getName_zh());
					}else{
						dictionary.setName(dictionary.getName_en());
					}
				}
			}*/
		} catch (RuntimeException e) {
			e.printStackTrace();
		}
		
		request.setAttribute("type", type);
		request.setAttribute("typeDetail", typeDetail);
		request.setAttribute("ctstype", ctstype);
		request.setAttribute("workKey", workKey);
		request.setAttribute("status", status);
		
		if(toOrder!=null && !toOrder.equals("")){
			request.setAttribute("list", list);
			return "cts/orderParam";
		}
		
		list = getPageList(list , request);
		request.setAttribute("list", list);
		return "cts/listCts";
	}
	
	@SuppressWarnings("unchecked")
	private <E>List<E> getPageList(List<E> list , HttpServletRequest request){
		pager = new Page();
		pager.setElements(list);
		pager.setPageNumber(pageNumber);
		pager.setPageSize(pageSize);
		pager.setTotalElements(list.size());
	   
		request.setAttribute("pageCount", pager.getLastPageNumber());
	    return pager.getCurrentElements();
	}
	/**
	 * 查找
	 * @return
	 */
	@Action("findDataDicationary")
	public String findDataDicationary(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String type = request.getParameter("type");
		String typeDetail = request.getParameter("typeDetail");
		String id = request.getParameter("id");
		
		if(CommUtils.isCorrectNumber(id)){
		   dataDictionary = this.commonManager.findEntityByPK(DataDictionary.class, Long.valueOf(id));
		}
		
		request.setAttribute("id", id);
		request.setAttribute("type", type);
		request.setAttribute("typeDetail", typeDetail);
		request.setAttribute("ctstype", "data");
		
		return "cts/addCts";
	}
	
	
	/**
	 * 保存
	 *
	 */
	@Action("saveDataDicationary")
	public void saveDataDicationary(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String dataId = request.getParameter("dataDictionary.id");
		if(dataDictionary!=null){
			if(CommUtils.isCorrectNumber(dataId)){
			  this.commonManager.modifyEntity(dataDictionary);
			  ActionUtil.outputData("2");
			}else{
			  this.commonManager.saveEntity(dataDictionary);
			  ActionUtil.outputData("1");
			}
		}else{
			ActionUtil.outputData("0");
		}
	}
	
	/**
	 * 保存 -- order
	 *
	 */
	@Action("saveDataOrder")
	public void saveDataOrder(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String param = request.getParameter("param");
		String type = request.getParameter("type");
		String typeDetail = request.getParameter("typeDetail");
		
		try {
			if(CommUtils.isCorrectNumber(type) && CommUtils.isCorrectNumber(typeDetail) && CommUtils.isValidStr(param)){
				String ids[] = param.split(",");
				for(int i = 0; i < ids.length; i++){
					long id = Long.parseLong(ids[i]);
					DataDictionary dd = this.commonManager.findEntityByPK(DataDictionary.class, id);
					dd.setOrderNo(i+1);
					this.commonManager.modifyEntity(dd);
				}
				
				ActionUtil.outputData("1");
			}else{
				ActionUtil.outputData("0");
			}
		} catch (Exception e) {
			ActionUtil.outputData("0");
			e.printStackTrace();
		}
	}
	/**
	 * 删除
	 *
	 */
	@Action("delDataDicationary")
	public void delDataDicationary(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String dataId = request.getParameter("id");
		
		int code = 0;
		if(CommUtils.isCorrectNumber(dataId)){
			DataDictionary data = this.commonManager.findEntityByPK(DataDictionary.class, Long.valueOf(dataId));
			List<Work> worklist  = this.dataDictionaryManager.findWorkByDicId(Long.valueOf(dataId));
			if(worklist.size() > 0){
				code = 2; // 被引用
			}else{
				this.commonManager.deleteEntity(data);
				code = 1;
			}
			ActionUtil.outputData(code);
		}else{
			ActionUtil.outputData(code);
		}
	}
	
	public IDataDictionaryManager getDataDictionaryManager() {
		return dataDictionaryManager;
	}

	public void setDataDictionaryManager(
			IDataDictionaryManager dataDictionaryManager) {
		this.dataDictionaryManager = dataDictionaryManager;
	}

	public DataDictionary getDataDictionary() {
		return dataDictionary;
	}

	public void setDataDictionary(DataDictionary dataDictionary) {
		this.dataDictionary = dataDictionary;
	}

	public CommonManager getCommonManager() {
		return commonManager;
	}

	public void setCommonManager(CommonManager commonManager) {
		this.commonManager = commonManager;
	}

	public String getKeyword() {
		return keyword;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

	public int getPageNumber() {
		return pageNumber;
	}

	public void setPageNumber(int pageNumber) {
		this.pageNumber = pageNumber;
	}

	public Page getPager() {
		return pager;
	}

	public void setPager(Page pager) {
		this.pager = pager;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	
	
	
}
